#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
# @Time    : 2021/3/7 11:21 下午
# @Author  : 白辉东
# @Email   : xyxk-huidong.bai@ximalaya.com
# @File    : logUtils.py
# @Software: PyCharm

import os
import re
from itertools import islice
import linecache

global lines


def open_log(path):
    global lines
    if not os.path.exists(path):
        return False
    with open(path, 'r', encoding='UTF-8') as f:
        text = f.read().splitlines()
    return text


def get_count_by_key(log, key):
    """
    返回关键日志出现的次数
    :param log: 日志文件
    :param key: 关键词
    :return: 出现的次数
    """
    count = 0
    for line in log:
        time = line.count(key)
        count += time
    return count


def is_exist(log, key):
    """
    关键字是否存在
    :param log: 日志文件
    :param key: 关键词
    :return: 关键字是否能被找到
    """
    flag = False
    if get_count_by_key(log, key):
        flag = True
    return flag


#
def get_datetime_by_key(log, key):
    """
    返回关键词的日志的时间
    :param log: 日志文件
    :param key: 关键词
    :return: 日志出现的时间节点
    """
    log_time = ''
    for line in log:
        if key in line:
            # 获取日志开头的datetime时间
            log_time = re.search(r'(?<=]\[).+(?=])', line).group().split(' ')[1]

    return log_time


def get_lines_by_key(log, key):
    """
    获取匹配关键词的日志行列表
    :param log: 日志文件
    :param key: 关键词
    :return: 获取匹配关键字的所有行的列表
    """
    results = []
    for line in log:
        if key in line:
            results.append(line)
    return results


def get_line_by_key(log, key):
    """
    获取匹配关键词最新的一整行的日志
    :param log:
    :param key:
    :return:
    """
    for line in log:
        if key in line:
            return line


def get_number_by_key(log, key):
    """
    获取关键字最新出现的行数n
    :param log: 日志文件
    :param key: 关键词
    :return: 关键字最新出现行数
    """
    n = 0
    for line in log:
        n += 1
        if key in line:
            return n


def get_numbers_by_key(log, key):
    """
    获取关键字出现的所有行数
    :param log: 日志文件
    :param key: 关键字
    :return: 关键字出现的所有行数number列表
    """
    results = []
    n = 0
    for line in log:
        n += 1
        if key in line:
            results.append(n)

    return results


def get_line_by_number(path, n):
    """
    获取日志文件指定行的内容
    :param path: 日志路径
    :param n: 行数
    :return: 指定行内容
    """
    return linecache.getline(path, n)


def get_lines_by_numbers(log, start, end):
    """
    获取指定某几行
    :param log: 日志文件
    :param start: 开始行数
    :param end: 结束行数
    :return: 多行日志
    """
    res = []
    for a in islice(log, start - 1, end):
        res.append(a)
    return res


def get_lines_by_tag(log, start_tag, end_tag):
    """
    根据起始行标记和结束行标记，获取两者之间的文本
    :param log: 日志文件
    :param start_tag: 起始节点
    :param end_tag: 结束节点
    :return: 两者之间的文本列表
    """
    n_start = get_number_by_key(log, start_tag)
    n_end = get_numbers_by_key(log, end_tag)[-1]
    return get_lines_by_numbers(log, n_start, n_end)


def get_key_by_tag(ln, start_tag, end_tag):
    """
    切片获取关键key
    :param ln: 某一行日志
    :param start_tag: 开始文本
    :param end_tag: 结束文本
    :return: key
    """
    return ln.split(start_tag)[1].split(end_tag)[0]


if __name__ == '__main__':
    test_path = 'C:\\Users\\BHD\\Desktop\\MideaMinAutoProject\\midea-mini-new-auto-test\\seriallogs\\cash_log\\rtos_cash.log'
    logs = open_log(test_path)

    print(get_line_by_number(test_path, 11))
    print(get_number_by_key(logs, 'WAKEUP  小雅小雅  detected'))
    print(get_numbers_by_key(logs, 'WAKEUP  小雅小雅  detected'))
    print(get_datetime_by_key(logs, 'WAKEUP  小雅小雅  detected'))
    print(get_count_by_key(logs, 'WAKEUP  小雅小雅  detected'))
    print(get_lines_by_tag(logs, '---domain', '---VADflag'))

    lines = '[DBG][2021-03-08 11:43:55.167 14963] OrionTransport read index: 8 bytesRead: 1600'
    print(get_key_by_tag(lines, 'DBG][', '3'))
    print(get_line_by_key(logs, '---query'))
    for i in get_lines_by_numbers(logs, 100, 120):
        print('-------' + i)

    print(get_line_by_key(logs, '---query:').split('---query:')[1].strip())
    print(get_line_by_key(logs, '---tts:').split('---tts:')[1].strip())
    print(bool(1 == 2))
    print(bool(get_line_by_key(logs, 'WAKEUP  小雅小雅  detected')))
